use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use yew_store::Update;

pub enum Mode {
    Login,
    Index,
}

impl Default for Mode {
    fn default() -> Self {
        Mode::Index
    }
}

#[derive(Default)]
pub struct State {
    pub mode: Mode,
    pub counter: isize,
}

pub enum Action {
    CounterInc,
    CounterDec,
}

#[derive(Clone, Debug)]
pub enum Event {
    CounterChanged,
    ModeChanged,
}

impl Update<State> for Action {
    type Event = Event;

    // 定义该操作如何修改全局状态
    fn update(self, state: &mut State) -> Option<Self::Event> {
        match self {
            Action::CounterInc => state.counter += 1,
            Action::CounterDec => state.counter -= 1,
        }

        Some(Event::CounterChanged)
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Error {
    pub error: String,
}

impl Error {
    pub fn new(msg: &str) -> Self {
        Self { error: msg.into() }
    }
}

impl From<failure::Error> for Error {
    fn from(error: failure::Error) -> Self {
        Error::new(&error.to_string())
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Post {
    pub id: usize,
    pub title: String,
    pub body: String,
}
